package com.cgsd.heyu.widgets.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cgsd.heyu.R;
import com.cgsd.heyu.ui.task.adapter.TaskClassifyAdapter;
import com.cgsd.heyu.ui.task.vo.TaskListVo;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

/**
 * @author xiaoxing
 * Description 举报
 * CreateData: 2020/11/3
 */
public class TaskClassifyPopup extends PopupWindow implements View.OnClickListener {

    private Context mContext;
    private RecyclerView mRecyclerView;
    private View myMenuView;
    private TaskClassifyAdapter classifyAdapter;

    public TaskClassifyPopup(Context context, OnClickTagListener listener) {
        this.mContext = context;
        this.onClickTagListener = listener;
        setPopupConfig();
        initAdapter();
    }

    /**
     * init adapter
     */
    private void initAdapter() {
        mRecyclerView = myMenuView.findViewById(R.id.mRecyclerView);
        mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3, LinearLayoutManager.VERTICAL, false));
        classifyAdapter = new TaskClassifyAdapter();
        mRecyclerView.setAdapter(classifyAdapter);
        classifyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                classifyAdapter.setSelected(position);
                TaskListVo.CatelistBean item = classifyAdapter.getItem(position);
                if (onClickTagListener != null) {
                    onClickTagListener.onClickTag(item);
                }
                dismiss();
            }
        });

//        List<String> list = new ArrayList<>();
//        list.add("全部");
//        list.add("投票助力");
//        list.add("投票助力");
//        list.add("投票助力");
//        list.add("投票助力");
//        list.add("投票助力");
//        list.add("投票助力");
//        list.add("投票助力");
//        list.add("投票助力");
//        list.add("投票助力");
//        classifyAdapter.setNewData(list);
    }

    /**
     * 设置popup的样式
     */
    private void setPopupConfig() {
        myMenuView = LayoutInflater.from(mContext).inflate(R.layout.dialog_task_classify, null);
        this.setContentView(myMenuView);
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(true);
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        myMenuView.findViewById(R.id.view_pop).setOnClickListener(this);
    }

    /**
     * 显示弹窗界面
     *
     * @param parent
     * @param catelist
     */
    public void show(View parent, List<TaskListVo.CatelistBean> catelist) {
        if (!this.isShowing()) {
            if (Build.VERSION.SDK_INT >= 19) {
                this.showAsDropDown(parent, 0, 10, Gravity.START);
            } else {
                this.showAsDropDown(parent, 0, 10);
            }
            classifyAdapter.setNewData(catelist);
        } else {
            this.dismiss();
        }
    }

    @Override
    public void onClick(View view) {
        dismiss();
    }

    public interface OnClickTagListener {
        void onClickTag(TaskListVo.CatelistBean bean);
    }

    public OnClickTagListener onClickTagListener;
}
